var path = require('path');
var mongoose = require('mongoose');
mongoose.Promise = global.Promise; //plugging in your own Promises Library


//define Models
function defineModels() {
    //define Schema
    const schemaObj = {
        showcase: {
            collection: 'uploadDataModel',
            model: {
                showcase_cover: String,
                showcase_name_label: String,
                showcase_name_value: String,
                showcase_tag_label: String,
                showcase_tag_value: String,
            }
        }
    }
    //showcase model
    const showcase_Schema = mongoose.Schema(schemaObj.showcase.model);
    const showcase_Model = mongoose.model(schemaObj.showcase.collection, showcase_Schema);
    // console.log('showcase_Model', showcase_Model);
    const modelObj = {
        showcase_Model
    }
    return modelObj;
}

//connect to DB
function connectToMongoDB() {
    mongoose.connect('mongodb://localhost/test');
    var db = mongoose.connection;
    db.once('open', () => {
        console.log('good to go!');
    })
        .on('error', (error) => {
            console.warn('Warning', error);
        });
    return db;
}

function storeUploadData(modelObj, data) {
    const options = {
        upsert: true,
    };
    const update = {
        showcase_cover: data.showcase_cover,
        showcase_name_label: data.showcase_name_label,
        showcase_name_value: data.showcase_name_value,
        showcase_tag_label: data.showcase_tag_label,
        showcase_tag_value: data.showcase_tag_value,
    }

    // console.log('storeUploadData data', data);
    // const uploadDataInstance = new modelObj.showcase_Model(update);
    // console.log("uploadDataInstance.id", uploadDataInstance.id);
    modelObj.showcase_Model.findOneAndUpdate({ showcase_name_value: data.showcase_name_value }, update, options)
    .then((err) => {
        if(err){
            console.log(err);
        }
    });


    // modelObj.showcase_Model.findByIdAndUpdate(uploadDataInstance.id, {
    //     showcase_cover: data.showcase_cover,
    //     showcase_name_label: data.showcase_name_label,
    //     showcase_name_value: data.showcase_name_value,
    //     showcase_tag_label: data.showcase_tag_label,
    //     showcase_tag_value: data.showcase_tag_value,
    // }, options).then((err) => {
    //     if(err){
    //         console.log(err)
    //     }
    // })
    // uploadDataInstance.save()
    //     .then((err) => {
    //         if (err) {
    //             console.log(err);
    //         }
    //     });

}

module.exports = function (app) {
    const db = connectToMongoDB();
    const modelObj = defineModels();

    app.get('/app/getAppData', (req, res) => {
        res.sendFile(path.join(__dirname, '../public/1.png'));
    });

    app.post('/app/postAppDataIntoDB', (req, res) => {
        storeUploadData(modelObj, req.body);
        res.send({ message: 'success' });
    });

    app.get('/app/getShowcaseData', (req, res) => {
        modelObj.showcase_Model.find({})
        .then((data) => {
                res.send({data, message: "success"});
        })
    });
}